import React from 'react'
// ===================================================================== Temp
import SubmitForm from '#cpt/temp/SubmitForm'
import Controls from '#cpt/temp/Controls'
// ===================================================================== global declare
const { $fn, $Lazy, $http } = window
// ===================================================================== $Lazy
const Box 		= $Lazy.load(()=>import('#tp/box/box'))
const message 	= $Lazy.file(()=>import('@antd/message'))
const $cache 	= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:{},
		specData:{},
		isEdit: true,
		isSweep:false,	// 是否扫码录入
		customHide:true,	// 控制自定义属性显示或隐藏
		formData: [
			{ label:'条形码', 		name:'spec_code', 			full:true,	required:true,	disabled:true,	autoFocus:false, 	
				onEnter: (value)=> { 
					$http.submit(null,'spec-case-info/info',{ param:{spec_code:value}}).then((data)=>{
						this.formRef.setValue(data)
						this.setState({specData:data})
						this.props.specData(data)
					})
				}
			},
			{ label:'送检医院', 		name:'hosp_name', 			full:true,	disabled:true, 		},
			{ label:'姓名', 		name:'patient_name', 		half:true,	required:true,	autoFocus:false, className:'patient_name'},
			{ label:'性别', 		name:'sex',					half:true, 	type:'select',	idStr: 'code', 	value:'45703',	isKeydown: 'sex',	 onValue: ({rows})=>{if(rows)this.submit.sex_name = rows.name}},
			{ label:'年龄一', 		name:'age_first_val', 		half:true},
			{ label:'年龄类型一', 	name:'age_first_type', 		half:true, 	type:'select',	idStr: 'code',	value: "30001",	onValue: ({rows})=>{if(rows)this.submit.age_first_type_name = rows.name}},
			{ label:'年龄二', 		name:'age_second_val', 		half:true},
			{ label:'年龄类型二', 	name:'age_second_type', 	half:true, 	type:'select',	idStr: 'code', 	value: "30003",	onValue: ({rows})=>{if(rows)this.submit.age_second_type_name = rows.name}},
			{ label:'送检科室', 		name:'department_name', 	half:true},
			{ label:'床号', 		name:'bed_no', 				half:true},
			{ label:'门诊/住院号', 	name:'outpatient', 			half:true},
			{ label:'送检医生', 		name:'doctor', 				half:true},
			{ label:'医生电话', 		name:'doctor_phone', 		half:true},
			{ label:'受检人电话', 	name:'patient_phone', 		half:true},
			{ label:'物流人员', 		name:'real_name', 			half:true,	required:true,	disabled:true},
			{ label:'受检人类型', 	name:'case_type', 			half:true,	type:'select',	idStr: 'code',	isKeydown: 'case_type'},
			{ label:'标本性状', 		name:'spec_traits', 		half:true,	type:'select',	idStr: 'code', required:true,	onValue: ({rows})=>{if(rows)this.submit.spec_traits_name = rows.name}},
			{ label:'采集类型',		name:'collect_type', 		half:true, 	type: 'radio', 	mode:'button',	value:'69002'},
			{ label:'医院标识', 		name:'hosp_identification', full:true},
			{ label:'证件类型', 		name:'card_type', 			full:true, 	type:'select',	value: '87007', idStr: 'code' },
			{ label:'证件号', 		name:'card_no', 			full:true },
			{ label:'采样日期',		name:'samp_date',  			half:true, 	type:'date-time', format:1,	submit:1,	showToday:false,	onValue:({value,formData})=>{
				if(value){
					$fn.disabledRows(formData, 'samp_time', false)
					this.setState({ formData })
				}
			} },
			{ label:'采样时间',		name:'samp_time',			half:true, 	type:'time' },
			{ label:'病理固定时间',	name:'bingli_coll_time',	full:true, 	type:'date-time',	format:2},
			{ label:'病理离体时间',	name:'bingli_take_time',	full:true, 	type:'date-time',	format:2 },
			{ label:'临床诊断', 		name:'diagnosis_info', 		full:true,	type:'textarea'},
			{ label:'识别标签',		name:'tags', 				full:true, 	type: 'checkboxGroup', idStr:'code' },
			{ label:'备注', 		name:'remark', 				full:true,	type:'textarea'},
			// 病人基本信息 增加5个自定义字段 2021-10-15  --马婷
			{ hide:true,	custom:true, onClick:()=>{this.customClick()}},
			{ label:'备用1', 		name:'user_defined1', 				full:true,	hide:true },
			{ label:'备用2', 		name:'user_defined2', 				full:true,	hide:true },
			{ label:'备用3', 		name:'user_defined3', 				full:true,	hide:true },
			{ label:'备用4', 		name:'user_defined4', 				full:true, 	hide:true },
			{ label:'备用5', 		name:'user_defined5', 				full:true, 	hide:true },
			{ label:'备用6', 		name:'user_defined6', 				full:true, 	hide:true },
			{ label:'备用7', 		name:'user_defined7', 				full:true, 	hide:true },
			{ label:'备用8', 		name:'user_defined8', 				full:true, 	hide:true },
			{ label:'备用9', 		name:'user_defined9', 				full:true, 	hide:true },
			{ label:'备用10', 		name:'user_defined10', 				full:true, 	hide:true },
		]
	}
	submit = {
		page_source: 'sp'
	}
	
	componentDidMount(){
		this.getSelect()
		$http.submit(this,'spec-case-info/getDoubleEntryEnabled', { loading:false }).then((data)=>{
			this.doubleEntryEnabled = data.enabled
		})
	}
	
	// 点击展开或关闭自定义属性
	customClick = () => {
		const { customHide, formData } = this.state
		this.setState({customHide:!customHide},()=>{
			$fn.hideRows(formData,['user_defined1','user_defined2','user_defined3','user_defined4','user_defined5','user_defined6','user_defined7','user_defined8','user_defined9','user_defined10'], this.state.customHide)
			this.setState({formData})
		})
	}
	
	setValue = ({spec_code}) => {
		$http.submit(null,'spec-case-info/checkEditEnabled',{ param:{spec_code,uuid:spec_code}}).then(({editEnabled})=>{
			if(editEnabled === '1'){
				this.setState({isEdit:true})
				message.then(f=>f.success(`请求成功`))
			}else{
				this.setState({isEdit:false})
				message.then(f=>f.error(`请前往双录入页面进行修改`))
			}
		}).then(
			$http.submit(null,'spec-case-info/info',{ param:{spec_code}}).then((data)=>{
				const { formData } = this.state
				// 采样日期不能大于接收日期
				const sampDate = $fn.getRow( formData, 'samp_date')
				const { check_time } = data
				if( check_time ){
					const check = check_time.split(' ')
					sampDate.defaultPickerValue = check[0]
					sampDate.after = check[0]
				}
				if(!data.samp_date){
					$fn.disabledRows(formData, 'samp_time', true)
				}else{
					$fn.disabledRows(formData, 'samp_time', false)
				}
				this.formRef.setValue(data)
				this.setState({formData, specData:data})
				setTimeout(()=>{document.querySelector('.patient_name input')?.focus?.()},500)
			})
		)
	}
	
	// 获取下拉数据
	getSelect = () => {
		const { formData } = this.state
		
		$cache.then( async f => {
			const dic = await f.getDicCache({ code: [ 45700, 21000, 42000, 69700, 30000, 87000 ]})
			// 标本性状
			$fn.setRow( formData, 'spec_traits', dic['21000'] )
			// 受检人类型
			$fn.setRow( formData, 'case_type', dic['42000'] )
			// 性别
			$fn.setRow( formData, 'sex', dic['45700'] )
			//识别标签
			$fn.setRow( formData, 'tags', dic['69700'] )
			// 年龄类型
			$fn.setRow( formData, 'age_first_type', dic['30000'] )
			$fn.setRow( formData, 'age_second_type', dic['30000'] )
			// 采集类型
			$fn.setRow( formData, 'collect_type', f.collect_type )
			// 身份证类型 [2021-8-17 3:19:24 ?F10: PM?] --伍智睿
			$fn.setRow( formData, 'card_type', dic['87000'])
			
			this.setState({ formData })
		})
	}
	
	controls = () => {
		// 保存 查看图片 审核 编辑
		return [
			Controls.click.call(this, '扫码录入', ()=>{ 
				const { formData } = this.state
				this.formRef.reset()
				$fn.disabledRows(formData, 'spec_code', false)
				$fn.getRow(formData, 'spec_code').autoFocus = true
				this.setState({formData,specData:[],isSweep:true}) 
			},{code: 'F7'} ),
			Controls.click.call(this, '仅保存', ()=>{this.onlySave()},{code: 'F8'} ),
			+this.doubleEntryEnabled === 0 && Controls.click.call(this, '保存并审核', ()=>{ this.saveAndAudit() },{code: 'F10'} ),
			Controls.click.call(this, '下一个', ()=>{ this.nextSpec() },{code: 'F11'} ),
		]
	}
	
	// 仅保存
	onlySave = ()=> {
		const { formData, isSweep } = this.state
		const param = this.formRef.getValue()
		const {pic_status,pic_uuid, spec_code, status} = this.state.specData
		if(!param.samp_date ) {param.samp_date = ''}
		if(!param.samp_time ) {param.samp_time = ''}
		this.submit.coll_time = param.samp_date + ' '+ param.samp_time
		if( pic_uuid === '' || pic_status === '43703'){
			$http.submit(null,'spec-case-info/keep',{ param:{entry_type: isSweep?'fast_scan':'fast',...this.state.specData,...this.submit,...param, status:status?status:'0'}}).then((data)=>{
				message.then(f=>f.success(`保存成功`))
				this.props.onRefresh()
				if(isSweep){
					this.formRef.reset()
					$fn.getRow(formData, 'spec_code').autoFocus = true
					this.setState({formData})
				}else{
					$fn.getRow(formData, 'patient_name').autoFocus = true
					this.setState({formData})
				}
			})
		}else{
			$http.submit(this,'app-upload-pic/audit',{ param:{uuid:pic_uuid,spec_code,status:'43703'}}).then(()=> {
				$http.submit(null,'spec-case-info/keep',{ param:{entry_type: isSweep?'fast_scan':'fast',...this.state.specData,...this.submit,...param,status:status?status:'0'}}).then((data)=>{
					message.then(f=>f.success(`保存成功`))
					this.props.onRefresh()
					if(isSweep){
						this.formRef.reset()
						$fn.getRow(formData, 'spec_code').autoFocus = true
						this.setState({formData})
					}else{
						$fn.getRow(formData, 'patient_name').autoFocus = true
						this.setState({formData})
					}
				})
			})
		}
		
		
	}
	// 保存并审核
	saveAndAudit = ()=> {
		const { formData, isSweep } = this.state
		const {pic_status,pic_uuid, spec_code, status} = this.state.specData
		const param = this.formRef.getValue()
		if(!param.samp_date ) param.samp_date = ''
		if(!param.samp_time ) param.samp_time = ''
		this.submit.coll_time = param.samp_date + ' '+ param.samp_time
		if( pic_uuid === '' || pic_status === '43703'){	// 图片已审核通过
			$http.submit(this,'spec-case-info/pass',{ param:{entry_type:'fast',...this.state.specData,...this.submit,...param,status:status?status:'0'}}).then((data)=>{
				message.then(f=>f.success(`保存并审核成功`))
				if(isSweep){
					this.formRef.reset()
					$fn.getRow(formData, 'spec_code').autoFocus = true
					this.setState({formData})
				}else{
					this.props.auditOnRefresh()
				}
			})
		}else {		// 图片待完善或待审核
			$http.submit(this,'app-upload-pic/audit',{ param:{uuid:pic_uuid,spec_code,status:'43703'}}).then((data)=>{
				$http.submit(this,'spec-case-info/pass',{ param:{entry_type: 'fast',...this.state.specData,...this.submit,...param,status:status?status:'0'}}).then((data)=>{
					message.then(f=>f.success(`保存并审核成功`))
					if(isSweep){
						this.formRef.reset()
						$fn.getRow(formData, 'spec_code').autoFocus = true
						this.setState({formData})
					}else{
						this.props.auditOnRefresh()
					}
				})
			})
		}
	}
	// 下一个
	nextSpec = ()=> {
		const { spec_code } = this.state.specData
		$http.submit(null,'spec-case-info/nextSpecCodeInfo',{ param:{spec_code}}).then((data)=>{
			this.setState({specData:data})
			this.formRef.setValue(data)
			this.props.specData(data)
		})
		
	}	
		
	render(){
		return (
			<>
				<Box title='信息修改' small full style={{height:'100%'}} scroll flex controls={this.controls()}>
					{
						SubmitForm.call(this,{ disabled:!this.state.isEdit, labelWidth:'s7', noholder:true, customHide: this.state.customHide,
							onForm:()=>{setTimeout(()=>{document.querySelector('.patient_name input')?.focus?.()},500)}
						} ) 
					}
				</Box>
			</>
		)
	}
}

export default Index